package cc.rico.tutorial;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by rico on 2018/8/19.
 */
public class ZkListenerDemo {
    private static final Logger logger = LoggerFactory.getLogger(ZkListenerDemo.class);
    private static String NODE_PATH = "/ZkListener";

    public static void main(String[] args) {
        ZkClient zkClient = new ZkClient("192.168.223.140:2181");

        zkClient.subscribeDataChanges(NODE_PATH, new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                logger.info("#handleDataChange.node = {}", s);
                logger.info("#handleDataChange.data = {}", o);
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {
                logger.info("#handleDataDeleted.node = {}", s);
            }
        });

        if(!zkClient.exists(NODE_PATH)) {
            zkClient.createPersistent(NODE_PATH);
        }
        zkClient.writeData(NODE_PATH, "1");
        zkClient.writeData(NODE_PATH, "2");

        zkClient.delete(NODE_PATH);
        zkClient.delete(NODE_PATH);

        zkClient.close();
    }


}
